package server;

import commons.Message;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class Connection extends Thread{
    private Socket socket;
    private ObjectInputStream in;
    private ObjectOutputStream out;

    public Connection(Socket socket){
        this.socket = socket;
        try {
            in = new ObjectInputStream(this.socket.getInputStream());
            out= new ObjectOutputStream(this.socket.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        /*try {
            while (true){
                Message message = (Message) in.readObject();
                StringBuilder msg = new StringBuilder();
                msg.append(message.getSender().getLogin());
                msg.append(": ");
                msg.append(message.getMessage());

                System.out.println(message.getSender().getLogin() + " : " + msg);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }*/
    }

    public void close(){
        try {
            in.close();
            out.close();
            socket.close();
            Server.connections.remove(this);
            System.out.println("End!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
